//
//  SISSPhotoBrowser.m
//  SISSAPP
//
//  Created by 优房网 on 2017/3/27.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSPhotoBrowser.h"
#import "SISSPhotoViewer.h"

@interface SISSPhotoBrowser ()<UIPageViewControllerDataSource>

/**
 photoViewer
 */
@property (weak, nonatomic)  SISSPhotoViewer *photoViewer;

@end

@implementation SISSPhotoBrowser

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupUI];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
}

- (void)back {
    [self dismissViewControllerAnimated:YES completion:nil];
}

/**
 设置UI
 */
- (void)setupUI {
    
    // 1.创建pageViewController
    UIPageViewController *pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:@{UIPageViewControllerOptionInterPageSpacingKey : @(20)}];
    
    // 2.添加pageViewController到当前controller
    [self addChildViewController:pageViewController];
    [self.view addSubview:pageViewController.view];
    [pageViewController didMoveToParentViewController:self];
    
    // 3.给pageViewController设置子控制器
    SISSPhotoViewer *photoViewer = [[SISSPhotoViewer alloc] init];
    photoViewer.index = self.index;
    photoViewer.image = self.images[self.index];
    photoViewer.url = self.urls[self.index];
    [pageViewController setViewControllers:@[photoViewer] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    self.photoViewer = photoViewer;
    
    // 4.设置数据源
    pageViewController.dataSource = self;
    
    // 5.添加手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];
    [pageViewController.view addGestureRecognizer:tapGesture];
    self.view.gestureRecognizers = pageViewController.gestureRecognizers;
}


/**
 手势点击事件
 */
- (void)tapGestureAction:(UITapGestureRecognizer *)tap {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UIPageViewControllerDataSource

/**
 返回前一个控制器
 
 @param pageViewController pageViewController
 @param viewController 当前的控制器
 @return 前一个控制器
 */
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    
    SISSPhotoViewer *photoViewer =(SISSPhotoViewer *)viewController;
    long currentIndex = photoViewer.index;
    
    if (currentIndex > 0) {
        SISSPhotoViewer *viewer = [[SISSPhotoViewer alloc] init];
        viewer.index = currentIndex - 1;
        viewer.image = self.images[currentIndex - 1];
        viewer.url = self.urls[currentIndex - 1];
        return viewer;
    }
    
    return nil;
}
/**
 返回后一个控制器
 
 @param pageViewController pageViewController
 @param viewController 当前的控制器
 @return 后一个控制器
 */
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    SISSPhotoViewer *photoViewer =(SISSPhotoViewer *)viewController;
    long currentIndex = photoViewer.index;
    
    if ((currentIndex < (self.urls.count - 1) && self.urls) || (self.images && currentIndex < (self.images.count - 1))) {
        SISSPhotoViewer *viewer = [[SISSPhotoViewer alloc] init];
        viewer.index = currentIndex + 1;
        viewer.image = self.images[currentIndex + 1];
        viewer.url = self.urls[currentIndex + 1];
        return viewer;
    }
    return nil;
}


@end
